//
//  HomePageCategoryViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HomePageCategoryViewController.h"
#import "FindCollectionCell.h"
#import "Header.h"

@interface HomePageCategoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *categoryCollection;
@property (nonatomic, strong) UICollectionViewFlowLayout *categoryLayout;


@end

@implementation HomePageCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"切换频道";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shangla"] style:UIBarButtonItemStylePlain target:self action:@selector(clickShangla)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];

    
    [self createCategory];
}

- (void)createCategory {
    self.categoryLayout = [[UICollectionViewFlowLayout alloc] init];
    //self.categoryLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 6, 44);
    self.categoryLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 3, 150);

    self.categoryLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.categoryLayout.minimumLineSpacing = 0;
    self.categoryLayout.minimumInteritemSpacing = 0;
    self.categoryLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 创建collection
    self.categoryCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:self.categoryLayout];
    [self.view addSubview:self.categoryCollection];
    //  设置代理
    self.categoryCollection.delegate = self;
    self.categoryCollection.dataSource = self;
    // 注册
    [self.categoryCollection registerClass:[FindCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FindCollectionCell class])];
    //边界回弹
    self.categoryCollection.bounces = NO;

}


#pragma mark collectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.categoryArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AGLCategory *category = self.categoryArr[indexPath.item];
        FindCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FindCollectionCell class]) forIndexPath:indexPath];
    cell.nameLabel.text = category.categoryName;
    [cell.picImageView setImageWithURL:[NSURL URLWithString:category.icon] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
    
        return cell;
}

- (void)clickShangla {
    
    // 动画
    CATransition *backPageAnimation = [CATransition animation];
    backPageAnimation.duration = 0.5;
    backPageAnimation.type = kCATransitionMoveIn;
    backPageAnimation.subtype = kCATransitionFromTop;
    
    [self.navigationController.view.layer addAnimation:backPageAnimation forKey:nil];
    
    [self.navigationController popViewControllerAnimated:NO];
}


#pragma mark 点击分类category
//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
#pragma mark 点击category block传值 3.
    // 后一个界面调用block
    self.block(indexPath.item);
    
    [self clickShangla];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
